package com.stylefeng.guns.rest.modular.keyword.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.stylefeng.guns.rest.common.persistence.dao.KeywordMapper;
import com.stylefeng.guns.rest.common.persistence.model.Keyword;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by ASUS-PC on 2018/6/24.
 */
@Service
public class KeywordService {
    @Autowired
    KeywordMapper keywordMapper;

    //获取最新的关键词
    public List<Keyword> getSearchKey(){
        return keywordMapper.searchKeyword();
    }
    //获取数量最多的10条数据
    public List<Keyword> getSearchKeywordList(){
        return keywordMapper.searchKeywordList();
    }

    //查询某个关键词是否存在
    public boolean searchKeywordIsIn(String keyword){
        List<Keyword> lists = keywordMapper.selectList(Condition.create().eq("keyword",keyword).eq("type",1));
        return lists.size()==0?false:true;
    }
    //插入关键词
    public int insertKeyword(String keyword){
        Keyword k = new Keyword();
        k.setType(1);
        k.setNum(1);
        k.setKeyword(keyword);
        k.setCreateTime(new Date());
        return keywordMapper.insert(k)>0 ? 1:0;
    }
    //已存在的关键词num+1
    public int updateNum(String keyword){
        List<Keyword> lists = keywordMapper.selectList(Condition.create().eq("keyword",keyword).eq("type",1));
        if(lists.size()>0){
            Keyword k = lists.get(0);
            k.setNum(k.getNum()+1);
            k.setCreateTime(new Date());
            return keywordMapper.updateById(k)>0?1:0;
        }
        return 0;
    }
}
